var app = getApp();
// 引入SDK核心类
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
  data: {
    admin: app.globalData.domain,
    errImg: app.globalData.errimg,
    userInfo: {},
    map: {},
    types: 0,//0 未授权  1 未选中经销商 
    storeList: [] //经销商
  },
  onLoad(options) {
    var that = this;
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'UQ6BZ-UH3WI-5UZG7-5EZVY-LRGUV-REBTI'
    });
    console.log(wx.getStorageSync('userInfo'))
    
  },

  getUserInfo: function (e) { //点击登录按钮
    var that = this;

    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (resUser) => {
        wx.login({
          success: function (res) {
            var code = res.code; // 微信登录接口返回的 code 参数，下面注册接口需要用到
            that.login(code, resUser.userInfo) // 调用后端接口存储用户数据
          }
        })
      },
      fail: function (res) {}
    })

  },

  login: function (code, user) { //调用登录接口
    var that = this;
    app.globalAjax(
      '/api/wechat/login',
      "get", {
        code: code,
        avatarUrl: user.avatarUrl,
        nickname: user.nickName,
        gender: user.gender
      },
      function (e) {
        console.log(e);
        if (e.code == 0) {
          wx.setStorage({
            key: "token",
            data: e.token //需要存储的数据
          })
          wx.setStorage({
            key: "userInfo",
            data: e.userInfo //需要存储的数据
          });         

          that.setData({
            types: 1,
            userInfo: e.userInfo
          })

        }
      }
    );
  },
  onShow: function () {
    let vm = this;
    vm.getUserLocation();
  },

  getUserLocation: function () {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        console.log(JSON.stringify(res))
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      vm.getLocation();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          vm.getLocation();
        } else {
          //调用wx.getLocation的API
          vm.getLocation();
        }
      }
    })
  },
  // 微信获得经纬度
  getLocation: function () {
    let vm = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(JSON.stringify(res))
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        vm.getLocal(latitude, longitude)
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },
  // 获取当前地理位置
  getLocal: function (latitude, longitude) {
    let vm = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        console.log(JSON.stringify(res));
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        vm.setData({
          map: {
            province: province,
            city: city,
            latitude: latitude,
            longitude: longitude
          }
        })
        wx.setStorage({
          key: "map",
          data: vm.data.map //需要存储的数据
        })
        //GET   获取经销商列表
        app.globalAjax(
          '/api/store/list',
          "get", {
            longitude: longitude,
            latitude: latitude
          },
          function (e) {
            if (e.code == 0) {
              vm.setData({
                storeList: e.storeList
              });
              console.log(e.storeList);
            }
          }
        );

      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });
  },
  errImg: function (e) {
    var _errImg = e.target.dataset.errImg;
    var _objImg = "'" + _errImg + "'";
    var _errObj = {};
    _errObj[_errImg] = this.data.errImg;
    console.log(e.detail.errMsg + "----" + _errObj[_errImg] + "----" + _objImg);
    this.setData(_errObj); //注意这里的赋值方式...
  },
  
  selectdor: function (event) { //选择经销商
    var that = this;
    console.log(event.currentTarget.dataset.index)
    var i = that.data.storeList[event.currentTarget.dataset.index]
    //设置缓存
    wx.setStorage({
      key: "dealer",
      data: i
    });

    wx.redirectTo({
      url: '/pages/index/index'
    })

  }
})